/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.codeminer.parts.database.dao;

import org.codeminer.database.DatabaseConnection;
import org.codeminer.database.DatabaseException;
import org.codeminer.parts.database.sql.From;
import org.codeminer.parts.database.sql.SQL;
import org.codeminer.parts.devices.StreamDevice;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public final class DeviceDAO extends DAO {

    private DeviceDAO() {
    }

    public static long store(StreamDevice device, DatabaseConnection connection) throws DatabaseException {
        connection.accuireWriteLock();
        try {
            updateAndGetNextId(connection, device);
            String insert = SQL.insertInto("Devices").values().value(device.getOid()).value(device.getIp()).value(device.getName()).toSQL();
            executeInsert(connection, insert);
            return device.getOid();
        } finally {
            connection.releaseWriteLock();
        }
    }

    public static List<StreamDevice> get(StreamDevice searchCriteria, DatabaseConnection connection) throws DatabaseException {
        ResultSet resultSet = null;
        connection.accuireReadLock();
        try {
            String query = null;
            From select = SQL.select().from("Albums");
            if (searchCriteria == null) {
                query = select.toSQL();
            } else if (searchCriteria.getOid() != -1) { //search by oid
                query = select.where("Device_Id").is(searchCriteria.getOid()).toSQL();
            } else if (searchCriteria.getName() != null) { //search by name
                query = select.where("Device_Name").is(searchCriteria.getName()).toSQL();
            } else if (searchCriteria.getIp() != null) { //search by ip
                query = select.where("Last_Ip").is(searchCriteria.getIp()).toSQL();
            }
            resultSet = connection.createStatement().executeQuery(query);
            List<StreamDevice> result = new LinkedList<>();
            while (resultSet.next()) {
                long oid = resultSet.getLong("Device_Id");
                String name = resultSet.getString("Device_Name");
                String ip = resultSet.getString("Last_Ip");
                 StreamDevice streamDevice = new StreamDevice(oid);
                streamDevice.setIp(ip);
                streamDevice.setName(name);
                result.add(streamDevice);
            }
            return result;
        } catch (SQLException ex) {
            throw new DatabaseException(ex);
        } finally {
            connection.releaseReadLock();
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException ignored) {
                }
            }
        }
    }

    public static boolean delete(StreamDevice streamDevice, DatabaseConnection connection) throws DatabaseException {
        validateIdentifiable(streamDevice);
        connection.accuireWriteLock();
        try {
            String deleteStatement = SQL.delete().from("Devices").where("Device_Id").is(streamDevice.getOid()).toSQL();
            return executeUpdate(connection, deleteStatement);
        } finally {
            connection.releaseWriteLock();
        }
    }

    public static boolean update(StreamDevice streamDevice, DatabaseConnection connection) throws DatabaseException {
        validateIdentifiable(streamDevice);
        connection.accuireWriteLock();
        try {
            String query = SQL.update("Devices").
                    set("Device_Name", streamDevice.getName()).
                    set("Device_Ip", streamDevice.getIp()).
                    where("Device_Id").is(streamDevice.getOid()).toSQL();
            return executeUpdate(connection, query);
        } finally {
            connection.releaseWriteLock();
        }
    }
}
